package com.Guigu.AIchat.controller;

import com.Guigu.AIchat.domain.ChatMessage;
import com.Guigu.AIchat.service.AIChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author 魏正想
 * @version 1.0
 * @date 2025/9/21  15:39
 */
/**
 * AI 聊天控制器，负责处理聊天请求并与第三方 AI 服务交互
 */
@RestController
@RequestMapping("/api/aichat")
public class AIChatController {
    @Autowired
    private AIChatService aiChatService;
//    @Value("${deepseek.api-key}")
    @Value("${deepseek.api.key}")
    /**
     * 用于存储调用第三方服务的密钥
     */
    private String secret;

    /**
     * 处理发送聊天消息的请求
     * @param key 前端请求头携带的密钥，用于接口鉴权
     * @param message 聊天消息体，包含文本或图片等内容
     * @return 返回 AI 处理结果或鉴权失败提示
     */
    @PostMapping("/send")
    public ResponseEntity<?> sendMessage(@RequestHeader("X-AI-Secret") String key,
                                         @RequestBody ChatMessage message) {
        // 校验密钥，防止未授权访问
        if (!secret.equals(key)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("密钥错误");
        }
        // 调用业务服务处理消息，并返回结果
        return ResponseEntity.ok(aiChatService.handleMessage(message));
    }

    @GetMapping("/history/{userId}")
    public ResponseEntity<?> getHistory(@RequestHeader("X-AI-Secret") String key,
                                        @PathVariable Long userId) {
        if (!secret.equals(key)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("密钥错误");
        }
        return ResponseEntity.ok(aiChatService.getHistory(userId));
    }
}
// ... existing code ...
